package uk.co.richardwkirk.parkrun.funnels.display;


import uk.co.richardwkirk.parkrun.funnels.model.FunnelSystem;

public abstract class FunnelDisplay {

	FunnelSystem funnels = null;
	long currentTime = 0;
	boolean funnelOverflowed = false;

	DisplaySettings displaySettings = null;
	
	public FunnelDisplay(DisplaySettings displaySettings) {
		this.displaySettings = displaySettings;
	}
	
	public void setTime(long time)
	{
		currentTime = time;
	}
	
	protected abstract void showTime(String time);

	/* (non-Javadoc)
	 * @see uk.co.richardwkirk.display.FunnelDisplay#update()
	 */
	public void update() {
		// We don't update status too often
		if (!displaySettings.isUpdateTime(currentTime)) {
			return;
		}
			
		if (!funnels.isEmpty()) {
			// Show the time
			showTime(convertTime(currentTime));
			
			// Show the state of the funnels
			showCurrentState();
		
			// Wait for the required display delay
			try {
				Thread.sleep(displaySettings.DELAY);
			}
			catch (Exception e) {
				// Nothing
			}
		}
	}

	protected abstract void showCurrentState();
	
	public abstract void setStatus(String status);

	public abstract void setOverflowed(boolean b);

	public void setFunnels(FunnelSystem funnels_) {
		this.funnels = funnels_;
	}
	
	protected String convertTime(long time)
	{
		StringBuilder convertedTime = new StringBuilder();
		int mins = (int)(time / 1000 / 60);
		int secs = (int)(time - (mins * 1000 * 60)) / 1000;
		convertedTime.append(mins).append(":");
		if (secs < 10)
		{
			convertedTime.append("0");
		}
		convertedTime.append(secs);
		return convertedTime.toString();
	}

}